package com.example.musicplayer.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.example.musicplayer.R;
import com.example.musicplayer.commonUtils.StringUtil;
import com.example.musicplayer.commonUtils.ToastUtil;
import com.example.musicplayer.dialogTools.NormalAlertDialog;
import com.example.musicplayer.provider.DBConstants;
import com.example.musicplayer.user.User;
import com.example.musicplayer.user.UserManage;

public class PasswordModifyActivity extends BaseSettingsActivity {
    public static final int RESULT_CODE = 700;
    public static final String MODIFY_SUCCESS = "modifySuccess";
    private EditText mInput;
    private EditText mInputAgain;
    private View mConfirm;
    private boolean mModifySuccess = false;
    //if forget password and modify
    private boolean mForgetPassword = false;
    private String mUserName;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mForgetPassword = getIntent().getBooleanExtra(LoginActivity.FORGET_PASSWORD, false);
        mUserName = getIntent().getStringExtra(DBConstants.User.NAME);
        if (mForgetPassword) {
            mHasLoginCheck = false;
        }
        super.onCreate(savedInstanceState);
        setEnableMusicTabView(false);
        loadEditView();
        setResult(RESULT_CODE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UserManage.updatePasswordConfirmState();
    }

    @Override
    protected int setActivityTitle() {
        return R.string.title_password_modify;
    }

    private void loadEditView() {
        LayoutInflater inflater = LayoutInflater.from(this);
        @SuppressLint("InflateParams") View editView = inflater.inflate(R.layout.password_modify_view, null);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT
                , RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        ((RelativeLayout) findViewById(R.id.relative_layout_base_settings)).addView(editView, lp);
        //edit text
        mInput = editView.findViewById(R.id.edittext_input_new_password);
        mInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                updateConfirmButton();
            }
        });
        mInputAgain = editView.findViewById(R.id.edittext_input_password_again);
        mInputAgain.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                updateConfirmButton();
            }
        });
        //confirm button
        mConfirm = editView.findViewById(R.id.button_confirm);
        mConfirm.setEnabled(false);
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doConfirmAction();
            }
        });
    }

    private void updateConfirmButton() {
        if (mInput.getText().toString().length() == 0 && mInputAgain.getText().toString().length() == 0) {
            mConfirm.setEnabled(false);
        } else {
            mConfirm.setEnabled(true);
        }
    }

    private void doConfirmAction() {
        String newPassword = mInput.getText().toString();
        if (newPassword.equals(mInputAgain.getText().toString())) {
            if (mForgetPassword) {
                if (UserManage.updatePasswordForForget(mUserName, StringUtil.ByteToHexString(newPassword.getBytes()))) {
                    mModifySuccess = true;
                    finishActivity();
                } else {
                    ToastUtil.makeToast(getString(R.string.password_modify_fail));
                }
            } else {
                if (User.getInstance() == null ||
                        !User.getInstance().setUserPassword(StringUtil.ByteToHexString(newPassword.getBytes()))) {
                    ToastUtil.makeToast(getString(R.string.password_modify_fail));
                } else {
                    mModifySuccess = true;
                    finishActivity();
                }
            }
        } else {
            mInput.setText("");
            mInputAgain.setText("");
            showErrorDialog();
        }
    }

    private void showErrorDialog() {
        NormalAlertDialog errorDialog = new NormalAlertDialog(this, getString(R.string.prompt)
                , getString(R.string.passwords_are_inconsistent));
        errorDialog.show();
    }

    @Override
    protected void finishActivity() {
        Intent intent = new Intent();
        intent.putExtra(MODIFY_SUCCESS, mModifySuccess);
        setResult(RESULT_CODE, intent);
        super.finishActivity();
    }
}